﻿/* -------------------------------------------------------------------------
//  File        :   tiles_pool.cpp
//  Author      :   Sum
//  Datet       :   2014/4/15 14:57
//  Brief       :   
//
// -----------------------------------------------------------------------*/
//
#include "stdafx.h"
#include "tiles_pool.h"
#include "game_path.h"


tiles_pool_x::tiles_pool_x()
{
    m_pHGE = NULL;
}

tiles_pool_x::~tiles_pool_x()
{
}

BOOL tiles_pool_x::init(HGE* pHge, LPCTSTR lpszFilePath)
{
    if (!pHge)
    {
        return FALSE;
    }

    m_pHGE = pHge;
    m_cstrGameFilePath = game_path::getGamePath();
    return _parseTilesXml(lpszFilePath);
}

BOOL tiles_pool_x::uninit()
{
    for (auto it : m_tiles_map)
    {
        if (it.second != NULL && m_pHGE)
        {
            m_pHGE->Texture_Free(it.second->GetTexture());
            delete it.second;
        }
    }

    return TRUE;
}

hgeSprite* tiles_pool_x::get(int nId)
{
    auto it = m_tiles_map.find(nId);
    if (it != m_tiles_map.end())
    {
        return it->second;
    }

    return NULL;
}

BOOL tiles_pool_x::_parseTilesXml(LPCTSTR lpszFilePath)
{
    BOOL bReturn = FALSE;
    TiXmlDocument doc;
    CString cstrPath(lpszFilePath);
    CString cstrRootName;
    TiXmlElement* pRoot = NULL;

    if (lpszFilePath == NULL)
    {
        goto exit;
    }

    if (!doc.LoadFile(CW2A(lpszFilePath).m_psz))
    {
        goto exit;
    }

    pRoot = doc.RootElement();
    if (pRoot == NULL)
    {
        goto exit;
    }

    cstrRootName = CA2W(pRoot->ValueStr().c_str()).m_psz;
    if (cstrRootName.IsEmpty()
        || cstrRootName.CompareNoCase(L"tiles") != 0)
    {
        goto exit;
    }

    for (TiXmlElement* pNote = pRoot->FirstChildElement();
        pNote != NULL;
        pNote = pNote->NextSiblingElement())
    {
        tiles_pool_item item;
        if (!_parseTilesXmlItem(item, pNote))
        {
            continue;
        }

        hgeSprite* pSprite = _createSprite(item);
        if (pSprite)
        {
            _insertItem(item, pSprite);
        }
    }

exit:
    return bReturn;
}

BOOL tiles_pool_x::_parseTilesXmlItem(tiles_pool_item& item, TiXmlElement* pNote)
{
    if (pNote == NULL)
    {
        return FALSE;
    }

    CString cstrItemName = CA2W(pNote->ValueStr().c_str()).m_psz;
    if (cstrItemName.CompareNoCase(L"i") != 0)
    {
        return FALSE;
    }

    item.cstrPath = m_cstrGameFilePath;
    item.cstrPath += CA2W(pNote->Attribute("path")).m_psz;

    pNote->Attribute("x", &item.x);
    pNote->Attribute("y", &item.y);
    pNote->Attribute("width", &item.nWidth);
    pNote->Attribute("height", &item.nHeigth);

    if (pNote->Attribute("id", &item.nId) == NULL
        || item.cstrPath.IsEmpty())
    {
        return FALSE;
    }

    return TRUE;
}

hgeSprite* tiles_pool_x::_createSprite(tiles_pool_item& item)
{
    if (m_pHGE == NULL)
    {
        return NULL;
    }

    HTEXTURE hTexture = m_pHGE->Texture_Load(CW2A(item.cstrPath).m_psz);
    if (hTexture == NULL)
    {
        return NULL;
    }

    return new hgeSprite(hTexture,
        (float)item.x,
        (float)item.y,
        (float)item.nWidth,
        (float)item.nHeigth);
}

BOOL tiles_pool_x::_insertItem(tiles_pool_item& item, hgeSprite* pSprite)
{
    auto it = m_tiles_map.find(item.nId);
    if (it != m_tiles_map.end())
    {
        delete it->second;
        it->second = pSprite;
    }
    else
    {
        m_tiles_map[item.nId] = pSprite;
    }

    return TRUE;
}

int tiles_pool_x::getTilesNum()
{
    return m_tiles_map.size();
}
